/* ----------------------------------------------------------------------------
* File: box_server.h
*
* Desc: Declaration of CBoxServer class
*
* Created: 19/02/2011, by Alexey Korshunov
* ---------------------------------------------------------------------------- */
#ifndef _BOX_SERVER_H_
#define _BOX_SERVER_H_

//#include "thread_pool.h"
#include "file_logger.h"
#include "map_thread_safe.h"
#include "user_box.h"


/************************************************************************/
/*	Structure to describe parameters of box server                      */
/************************************************************************/
typedef struct box_server_params
{
	int control_port;
	int data_port;
	int max_connections;
	int use_syslog;
	int use_thread_pool;
	int auth;

	box_server_params():
		control_port(0),
		data_port(0),
		max_connections(0),
		use_syslog(0),
		use_thread_pool(0),
		auth(0) {}

	box_server_params& operator=(box_server_params& params)
	{
		if (this == &params)
			return *this;
		control_port = params.control_port;
		data_port = params.data_port;
		max_connections = params.max_connections;
		use_syslog = params.use_syslog;
		use_thread_pool = params.use_thread_pool;
		auth = params.auth;
		return *this;
	}
}box_server_params;


class CBoxServer;
typedef struct server_env
{
	server_env() : listen_data_sock(-1), listen_control_sock(-1), server(NULL), logger(NULL),
		stop(true)
	{
		pthread_cond_init(&cv_start, NULL);
		pthread_mutex_init(&mutex_start, NULL);

		pthread_cond_init(&cv_stop, NULL);
		pthread_mutex_init(&mutex_stop, NULL);
	}

	~server_env()
	{
		pthread_cond_destroy(&cv_start);
		pthread_mutex_destroy(&mutex_start);

		pthread_cond_destroy(&cv_stop);
		pthread_mutex_destroy(&mutex_stop);
	}

	int listen_data_sock;
	int listen_control_sock;
	CBoxServer *server;
	CFileLogger *logger;
	bool stop;

	pthread_cond_t cv_start;
	pthread_mutex_t mutex_start;

	pthread_cond_t cv_stop;
	pthread_mutex_t mutex_stop;
}server_env;

/************************************************************************/
/*	CBoxServer class to control users boxes                             */
/************************************************************************/
class CUserBox;
class CBoxConnection;
class CMapConnection;
class CMapUserBox;
class CBoxServer
{
public:
	CBoxServer();
	~CBoxServer();

	// Init server
	void Init(box_server_params& params) throw(std::exception);
	// Start server
	void Start() throw(std::exception);
	// Stop server
	void Stop() throw(std::exception);

	// Get logger object
	CFileLogger* GetLogger() const { return m_logger; }
	// Get map connections
	CMapConnection* GetMapConnection() const { return m_map_conn; }

	// Attach user box
	CUserBox* AttachUserBox(const char *username, const CBoxConnection *conn);
	// Detach user box
	int DetachUserBox(unsigned int id_box, CBoxConnection *conn);
	// Find box by user name
	CUserBox* FindOpenedBoxByUserName(const char *username);
	// Get count opened user boxes
	size_t GetCountOpenedBoxes() const;

	// Open connection
	CBoxConnection* OpenConnection(int sock, const struct sockaddr_in *remoteaddr);
	// Close connection
	void CloseConnection(unsigned int id_conn);
	
	// Get params
	const box_server_params* GetParams() const { return &m_params; }

private:
	// Read config file
	void ReadConfig(const char *file_name);
	// Check parameters
	int CheckParams(box_server_params& params);
	// Log entry
	static void LogEntry(const char*,...);


	server_env m_env;
	pthread_t m_thread_id;
	bool m_init;

	box_server_params m_params;

	//thread_pool_tt* m_thread_pool;		// thread pool for client connections

	CMapConnection *m_map_conn;
	CMapUserBox *m_map_boxes;

	static CFileLogger* m_logger;

	pthread_mutex_t m_mutex_open_box;
};


#endif // _BOX_SERVER_H_